# Saying Thanks and Meaning It: Expressing Gratitude for Social Gain
###### Analysis code
###### Peter Kearns


###############################libraries################################
install.packages("apaTables")
library(apaTables)

install.packages("psych")
library(psych)

###############################Study 1###################################
#reading study 1 data into R
#dataset for Study 1 will be refered to as grat1
grat1<- read.csv("study 1.csv")

#this line defines primary computed variables
to.analyze <- c("gq6", "GAC", "BIDRsde","BIDRim", "lifesat")
  
#apa tables was used to compute correlations, means and standard deviations for all study variables
apa.cor.table(grat1[to.analyze], filename= "table1.docx")


#At this point the Hayes Process Macro code (process.r) should be run, to execute the code that creates the process() function

#four mediation models were tested:

process(y = "lifesat", m ="GAC", x = "BIDRim",data=grat1, model = 4, boot = 10000)

process(y = "lifesat", m ="gq6", x = "BIDRim",data=grat1, model = 4, boot = 10000)


process(y = "lifesat", m ="GAC", x = "BIDRsde",data=grat1, model = 4, boot = 10000)

process(y = "lifesat", m ="gq6", x = "BIDRsde",data=grat1, model = 4, boot = 10000)

###############################Study 2###################################

#dataset for Study 2 will be referred to as grat2
grat2<- read.csv("study 2.csv")

#this line defines primary computed variables
to.analyze2 <- c("gq6", "GAC", "BIDRsde","BIDRim","intrinsic", "extrinsic", "lifesat")

#apa tables was used to compute and write correlations, means and standard deviations for all study variables
apa.cor.table(grat2[to.analyze2], filename= "table2.docx")


#testing effects of gratitude, motivation and their interaction on life satisfaction
gacXintrin.mod<- lm(lifesat ~GAC*intrinsic, data = grat2)
summary(gacXintrin.mod)

gacXextrin.mod<- lm(lifesat ~GAC*extrinsic, data = grat2)
summary(gacXextrin.mod)

gq6Xintrin.mod<- lm(lifesat ~gq6*intrinsic, data = grat2)
summary(gq6Xintrin.mod)

gq6Xextrin.mod<- lm(lifesat ~gq6*extrinsic, data = grat2)
summary(gq6Xextrin.mod)

#probing significant interaction
describe(grat2$extrinsic)
#Extrinsic Mean and SD:
  #mean = 5.17
  #sd = 2.35

#high(+1sd)
#grat2[grat2$extrinsic>7.52,]

#low (-1sd)= 
#grat2[grat2$extrinsic<2.82,]

#effect when extrinsic is high
probe1<- lm(lifesat ~ gq6, data = grat2[grat2$extrinsic>7.52,])
summary(probe1)

#effect when extrinsic is low
probe2<- lm(lifesat ~ gq6, data = grat2[grat2$extrinsic<2.82,])
summary(probe2)


#repeated measures anova test was conducted in JASP

